//
//  UtraMonitorThread.cpp
//  testWX
//
//  Created by 李 坚 on 12-12-27.
//  Copyright (c) 2012年 lijian. All rights reserved.
//

#include "UtraMonitorThread.h"
#include <iostream>
#include <mm_malloc.h>
#include "CharChangeUtil.h"
using namespace std;

UtraThreadEvent::UtraThreadEvent(wxEventType eventType, int id,char* data,int size):wxThreadEvent(eventType,id),m_size(size){
    bzero(m_dataBuff, sizeof(m_dataBuff));
    memcpy(m_dataBuff, data,size);
}

UtraMonitorThread:: UtraMonitorThread(Uart * utra,MainFrame* frame):m_start(false){
    this->utra=utra;
    this->frame=frame;
}

void *UtraMonitorThread::Entry(){
    char buff[1024];
    int size=0;
    m_start=true;
    while (m_start) {
        bool rtn=utra->readData((unsigned char*)buff, &size);
        if(rtn==true){
//            if(hexFormat){
//                string str=CharChangeUtil::hexToString(buff, size);
//                wxString wxs=wxString(str);
//                UtraThreadEvent event(wxEVT_THREAD,WORKER_EVENT,wxs,size);
//                wxQueueEvent(this->frame,(wxEvent*)event.Clone());
//            }else{
//                const char* a=(const char*)buff;
//                string str(a);
//                printf("%s\n",a);
//                wxString wxs=wxString(str);
//                UtraThreadEvent event(wxEVT_THREAD,WORKER_EVENT,wxs,size);
//                wxQueueEvent(this->frame,(wxEvent*)event.Clone());
//            }
            UtraThreadEvent event(wxEVT_THREAD,WORKER_EVENT,buff,size);
            wxQueueEvent(this->frame,(wxEvent*)event.Clone());
        }
    }
    cout<<"thread stop!"<<endl;
    return NULL;
}

void UtraMonitorThread::stop(){
    m_start=false;
    utra->close();
    cout<<"utrl closed!"<<endl;
}